<?php
namespace Admin\Controller;


class CommentController extends AdminController 
{
    private $_comment = null;

    public function _initialize()
    {
      parent::_initialize();
    	$this->_comment = D('comment');
    }

    public function index()
    {
    	$search = I('get.phone');
    	// echo $search;exit;
    	if(empty($search)) {
    		//分页查询
    		$count = $this->_comment->count();// 查询满足要求的总记录数
    		// var_dump($count);exit;
    		$Page = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(3)
    		$show = $Page->show();// 分页显示输出
			$sql = "select u.name as username,u.phone,m.name,co.content,co.id,co.addtime from lamp_user u join lamp_comment co on u.id=co.uid join lamp_movie m on co.mid=m.id";
			$data = M('user')->field('lamp_user.name as username,lamp_user.phone,m.name,co.content,co.id,co.addtime')->join('lamp_comment co on lamp_user.id=co.uid')->join('lamp_movie m on co.mid=m.id')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			// echo $data;exit;
			// $list = M()->page($_GET['p'],'3')->query($sql);
			// var_dump($list);exit;

		   //分配变量
			$this->assign('page',$show);
			$this->assign('list',$data);
		   //加载模板
	       $this->display();
	       exit;
    	}
			//分页查询
    		$count = M('comment')->field('m.name,u.phone')->join('lamp_user u on u.id=lamp_comment.uid' )->join('lamp_movie m on lamp_comment.mid = m.id where u.phone like "%'.$search.'%"')->count('lamp_comment.id');
			// $count = $this->_comment->where('name="%'.$search.'%"')->count();// 查询满足要求的总记录数
			// var_dump($count);exit;
			$Page = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(3)
			$show = $Page->show();// 分页显示输出
			
			$data = M('comment')->field('m.name,lamp_comment.addtime,lamp_comment.id,lamp_comment.content,u.name as username,u.phone')->join('lamp_user u on u.id=lamp_comment.uid' )->join('lamp_movie m on lamp_comment.mid = m.id where u.phone like "%'.$search.'%"')->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			// var_dump($data);exit;

		   //分配变量
			$this->assign('page',$show);
			$this->assign('list',$data);
		   //加载模板
	       $this->display();

		// ->where('name="%'.$search.'%"')
    }

    //加载修改页面
    public function edit(){
    	//查出数据
    	$sql = "select u.name as username,u.phone,m.name,co.content,co.id,co.addtime from lamp_user u join lamp_comment co on u.id=co.uid join lamp_movie m on co.mid=m.id where co.id=".I('id');
		$list = M()->query($sql);
    	// var_dump($list);
    	//向模板分配数据
    	$this->assign('list',$list);
    	//加载模板
    	$this->display();
    }

   //执行修改操作
   public function save()
   {
   	// var_dump($this->_comment->create());exit;
   	//初始化
   	if(!$this->_comment->create()){
   		$this->error($this->_comment->getError());
   		exit;
   	}
	// var_dump($this->_comment->fetchSql()->save());exit;
   	//执行修改 
   	if($this->_comment->save() >= 0){
   		$this->success("修改成功！",U('Comment/index'));
   	}else{
   		$this->error("修改失败");
   	}
   }

   //删除操作
   public function del(){
   	//把用户角色表中相关的也删除
   	if($this->_comment->delete($_GET['id'])>0){
   		$this->success("删除成功！",U('Comment/index'));
   	}else{
   		$this->error("删除失败");
   	}
   }
}